package com.newviews.tcmip.common.utils;


import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * 类描述：时间操作定义类
 *
 * @Author: ChenLong
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    /**
     * 默认日期时间格式
     */
    public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     * 默认日期格式
     */
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    /**
     * 默认时间格式
     */
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    /**
     * 获取一天的开始时间
     */
    public static LocalDateTime getMinDateTime(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            localDateTime = LocalDateTime.now();
        }
        return LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth(), 0, 0, 0, 0);
    }

    /**
     * 获取一天的开始时间
     */
    public static LocalDateTime getMinDateTime(LocalDate localDate) {
        if (localDate == null) {
            localDate = LocalDate.now();
        }
        return LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(), 0, 0, 0, 0);
    }

    /**
     * 获取一天的结束时间
     */
    public static LocalDateTime getMaxDateTime(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            localDateTime = LocalDateTime.now();
        }
        return LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth(), 23, 59, 59, 999999999);
    }

    /**
     * 获取一天的结束时间
     */
    public static LocalDateTime getMaxDateTime(LocalDate localDate) {
        if (localDate == null) {
            localDate = LocalDate.now();
        }
        return LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(), 23, 59, 59, 999999999);
    }


    /**
     * 计算XX小时之后的时间
     *
     * @param localDateTime 原始时间
     * @param hour          增加的小时说
     * @return 变化账号的时间
     */
    public static LocalDateTime computeAdditionHour(LocalDateTime localDateTime, Integer hour) {
        if (localDateTime == null) {
            localDateTime = LocalDateTime.now();
        }
        return localDateTime.plusHours(hour);
    }

    /**
     * localDateTime 转字符串
     *
     * @param localDateTime 原始时间
     * @return 变化账号的时间
     */
    public static String localDateTimeToStr(LocalDateTime localDateTime, boolean isNull) {
        if (localDateTime == null) {
            return isNull ? null : "";
        }
        return DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT).format(localDateTime);
    }


    /**
     * 比较日期
     */
    public static boolean equals(LocalDate d1, LocalDate d2) {

        if (d1 == d2) {
            return true;
        }
        if (d1 == null || d2 == null) {
            return false;
        }
        return d1.compareTo(d2) == 0;
    }

    /**
     * 比较日期
     */
    public static boolean equals(LocalDateTime d1, LocalDateTime d2) {
        if (d1 == d2) {
            return true;
        }
        if (d1 == null || d2 == null) {
            return false;
        }
        return d1.compareTo(d2) == 0;
    }


    /**
     * 比较日期
     */
    public static boolean equals(LocalTime d1, LocalTime d2) {
        if (d1 == d2) {
            return true;
        }
        if (d1 == null || d2 == null) {
            return false;
        }
        return d1.compareTo(d2) == 0;
    }

    /**
     * 系统日期时间-字符串（yyyy-MM-dd HH:mm:ss）
     */
    public static String getCurrTimeStr() {
        return DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT).format(LocalDateTime.now());
    }

    /**
     * LocalDateTime -> 毫秒
     * 竟然加了8小时
     *
     * @param localDateTime localDateTime
     * @return 毫秒
     */
    public static long toMilliSecond(LocalDateTime localDateTime) {
        // 比标准实际慢8小时，就是当前的时间了。
        return localDateTime.toInstant(ZoneOffset.ofTotalSeconds(OFFSET)).toEpochMilli();
    }

    /**
     * 8小时的秒数
     */
    private static final int OFFSET = 8 * 60 * 60;

    public static void main(String[] args) {
        String st1 = null, st2 = null, st3 = null, st4 = null;
        LocalDateTime startTime, endTime, currTime, lastTime;
        st1 = "2022-11-21 01:54:33.030";
        st2 = "2022-11-27 01:54:33.138";
        st3 = "2022-11-27 01:54:33.030";
        st4 = "2022-11-27 01:54:33.029";

        //  2022-11-27 01:54:33.030
        startTime = LocalDateTime.parse(st1, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
        endTime = LocalDateTime.parse(st2, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
        currTime = LocalDateTime.parse(st3, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
        lastTime = LocalDateTime.parse(st4, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));

        System.out.println(toMilliSecond(startTime));
        System.out.println(toMilliSecond(endTime));
        System.out.println(toMilliSecond(currTime));
        System.out.println(toMilliSecond(lastTime));
        System.out.println(lastTime.plusMinutes(5));

        System.out.println(Duration.between(startTime, endTime).toMinutes());

        System.out.println(("dbo.EMR_BASY_ZZJH".replace(".", "_") + "_CT").toUpperCase());
    }
}